//
//  SEIncomeViewController.swift
//  SmartEyeForDoctor
//
//  Created by QDSG on 2021/4/20.
//

import UIKit

class SEIncomeViewController: SEBaseViewController {
    
    var viewModel = SEIncomeViewModel()
    
    override func setupUI() {
        super.setupUI()
        
        view.backgroundColor = UIColor.white
        
        view.addSubview(headerView)
        view.addSubview(tableView)
        
        headerView.snp.makeConstraints { (make) in
            make.top.left.right.equalToSuperview()
            make.height.equalTo(UIScreen.statusBarHeight + 180)
        }
        
        tableView.snp.makeConstraints { (make) in
            make.top.equalTo(headerView.snp.bottom)
            make.left.right.bottom.equalToSuperview()
        }
    }
    
    override func setupNavBar() {
        super.setupNavBar()
        navigationController?.setNavigationBarHidden(true, animated: true)
    }
    
    private lazy var tableView: UITableView = {
        let tableView = UITableView()
        tableView.backgroundColor = .clear
        tableView.tableHeaderView = UIView(frame: CGRect(x: 0, y: 0, width: 0, height: 14))
        tableView.tableFooterView = UIView()
        tableView.isScrollEnabled = false
        tableView.rowHeight = 54
        tableView.separatorColor = UIColor(hex: "#EEEEEE")
        tableView.separatorInset = UIEdgeInsets(top: 0, left: 15, bottom: 0, right: 15)
        tableView.delegate = self
        tableView.dataSource = self
        tableView.register(SEIncomeEntranceCell.nibForCell(),
                           forCellReuseIdentifier: SEIncomeEntranceCell.reuseIdentifier())
        return tableView
    }()
    
    lazy var headerView = UINib.loadViewFrom(SEIncomeHeaderView.self)
    
}

// MARK: - UITableViewDataSource, UITableViewDelegate

extension SEIncomeViewController: UITableViewDataSource, UITableViewDelegate {
    
    // MARK: - UITableViewDataSource
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return viewModel.entranceModels.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: SEIncomeEntranceCell.reuseIdentifier(), for: indexPath) as! SEIncomeEntranceCell
        cell.updateCell(viewModel.entranceModels[indexPath.row])
        return cell
    }
    
    // MARK: - UITableViewDelegate
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if indexPath.row == 0 {
            let vc = SEIncomeListViewController()
            navigationController?.pushViewController(vc, animated: true)
        }
        if indexPath.row == 1 {
            let vc = SEIncomeBankViewController()
            navigationController?.pushViewController(vc, animated: true)
        }
    }
}
